<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle">
      <span class="placeholder" v-if="!location">请选择配送地址</span>
      <span class="value" v-else>{{ location }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="isShow">
      <template v-if="cityList">
        <span
          class="ellipsis"
          v-for="item in list"
          :key="item.code"
          @click="selectCityData(item)"
          >{{ item.name }}</span
        >
      </template>
      <template v-else>
        <div class="loading"></div>
      </template>
    </div>
  </div>
</template>
<script>
import { ref, reactive, computed } from "vue";
import { onClickOutside } from "@vueuse/core";
import axios from "axios";
export default {
  name: "XtxCity",
  setup(props, { emit }) {
    // 控制下拉菜单是否显示
    const isShow = ref(false);
    // 获取元素
    const target = ref();
    // 用于保存数据
    const cityList = ref(null);
    // 用于保存显示的地址
    const location = ref();
    // 用于保存选择的地址信息
    const selectCityList = reactive({
      provinceCode: "",
      provinceName: "",
      cityCode: "",
      cityName: "",
      countyCode: "",
      countyName: "",
    });
    // 选择地址并保存
    const selectCityData = (item) => {
      // 判断选择的是市级区及县级 // level为0 则是市级,1为区级,2为县级
      if (item.level === 0) {
        // 保存省级数据
        selectCityList.provinceCode = item.code;
        selectCityList.provinceName = item.name;
      } else if (item.level === 1) {
        // 保存市级数据
        selectCityList.cityCode = item.code;
        selectCityList.cityName = item.name;
      } else {
        // 保存区级数据
        selectCityList.countyCode = item.code;
        selectCityList.countyName = item.name;
      }
    };

    // 计算属性监听selectCityData,当数据发生变化,重新渲染
    const list = computed(() => {
      // 默认显示数据为省级数据
      let list = cityList.value;
      // 判断有省级数据,则显示区级数据
      if (selectCityList.provinceCode) {
        list = list.find(
          (item) => item.code === selectCityList.provinceCode
        ).areaList;
      }
      // 判断如果有市级数据,则模板显示区级数据
      if (selectCityList.cityCode) {
        list = list.find(
          (item) => item.code === selectCityList.cityCode
        ).areaList;
      }
      // 判断如果有县级数据,则关闭模态框
      if (selectCityList.countyCode) {
        // 选择完毕,将储存的地址数据传出父组件
        const { provinceCode, cityCode, countyCode } = selectCityList;
        list = cityList.value;
        hide();
        emit("onCityChange", {
          provinceCode,
          cityCode,
          countyCode,
          location: location.value,
        });
      }
      return list;
    });
    // 点击配送地址触发事件;
    const toggle = () => {
      // 点击下拉菜单,获取数据
      getCityList().then((res) => {
        cityList.value = res;
      });
      show();
    };
    // 显示模态框
    const show = () => {
      isShow.value = true;
    };
    // 隐藏模态框
    const hide = () => {
      isShow.value = false;
      //保存地址并在模板显示
      location.value = `${selectCityList.provinceName} ${selectCityList.cityName} ${selectCityList.countyName}`;
      // selectCityList.provinceName +
      // " " +
      // selectCityList.cityName +
      // " " +
      // selectCityList.countyName;
      // 重置用户储存数据(通过reactive定义的数据不能直接重置)
      for (let key in selectCityList) {
        selectCityList[key] = "";
      }
    };
    // 在下拉菜单外点击,隐藏下拉菜单
    onClickOutside(target, () => {
      if (isShow.value) {
        hide();
      }
    });
    return {
      isShow,
      toggle,
      target,
      cityList,
      selectCityData,
      list,
      location,
    };
  },
};
window.cityData = null;
//  获取城市数据
async function getCityList() {
  // 判断window中是否有数据, 有则直接返回数据
  if (window.cityData) return window.cityData;
  const res = await axios.get(
    "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
  );
  return (window.cityData = res.data);
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
